#define _CRT_SECURE_NO_WARNINGS 1
#include "stack.h"

void StackInit(Stack* ps)
{
	assert(ps);
	ps->data = NULL;
	ps->capaciyt = 0;
	ps->size = 0;
}

void StackDestroy(Stack* ps)
{
	assert(ps);

	free(ps->data);
	ps->data = NULL;
	ps->capaciyt = ps->size = 0;
}

void StackPush(Stack* ps, STDataType x)
{
	assert(ps);

	if (ps->size == ps->capaciyt)
	{
		int newCapacity = ps->capaciyt == 0 ? 4 : ps->capaciyt * 2;
		STDataType* tmp = (STDataType*)realloc(ps->data, sizeof(STDataType) * newCapacity);
		if (tmp == NULL)
		{
			perror("realloc");
			exit(-1);
		}
		ps->data = tmp;
		ps->capaciyt = newCapacity;
	}

	ps->data[ps->size] = x;
	ps->size++;
}

void StackPop(Stack* ps)
{
	assert(ps);
	assert(!StackEmpt(ps));

	ps->size--;
}

STDataType StackTop(Stack* ps)
{
	assert(ps);
	assert(!StackEmpt(ps));

	return ps->data[ps->size - 1];
}

bool StackEmpt(Stack* ps)
{
	assert(ps);

	return ps->size == 0;
}

int StackSize(Stack* ps)
{
	assert(ps);

	return ps->size;
}